package com.ems.controller.supers.questions;

import com.ems.pojo.data.ResultData;
import com.ems.pojo.data.RetunResult;
import com.ems.pojo.questions.Grade;
import com.ems.pojo.questions.Subject;
import com.ems.pojo.questions.Tlevel;
import com.ems.service.supers.questions.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: 伟伟
 * @Date: 2019/10/16
 * @Description: 对于学历，年级，客户的控制类
 * @Version: 1.0
 */
@Controller
@CrossOrigin //允许所有请求跨域
@RequestMapping("/subjectController")
public class SubjectController {
    @Autowired
    @Qualifier("subjectService")
    private SubjectService subjectService;

    /**
     * 获取所有试题种类
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAllSubject")
    public ResultData getAllSubject(){
        return RetunResult.AssemblyData(subjectService.getAllSubject());
    }


    /**
     * 添加学历级别
     */
    @ResponseBody
    @RequestMapping("/addLevel")
    public ResultData addLevel(@RequestBody Tlevel tlevel){
        subjectService.addLevel(tlevel);
        return RetunResult.AssemblyData();
    }


    /**
     * 删除学历级别
     */
    @ResponseBody
    @RequestMapping("/deleteLevel")
    public ResultData deleteLevel(@RequestBody Tlevel tlevel){
        List<Grade> list = subjectService.findAllGrade(tlevel);
        if(list.size() <= 0){
            subjectService.deleteLevel(tlevel);
            return RetunResult.AssemblyData();
        }else{
            return RetunResult.AssemblyData("有关联的年级，不能删除");
        }
    }


    /**
     * 添加年级别
     */
    @ResponseBody
    @RequestMapping("/addGrade")
    public ResultData addGrade(@RequestBody Grade grade){
        subjectService.addGrade(grade);
        return RetunResult.AssemblyData();
    }


    /**
     * 删除年级级别
     */
    @ResponseBody
    @RequestMapping("/deleteGrade")
    public ResultData deleteGrade(@RequestBody Grade grade){
        List<Subject> list = subjectService.findAllSubject(grade);
        if(list.size() <= 0){
            subjectService.deleteGrade(grade);
            return RetunResult.AssemblyData();
        }else{
            return RetunResult.AssemblyData("有关联的科目，不能删除");
        }
    }

    /**
     * 添加科目
     */
    @ResponseBody
    @RequestMapping("/addSubject")
    public ResultData addSubject(@RequestBody Subject subject){
        subjectService.addSubject(subject);
        return RetunResult.AssemblyData();
    }

    /**
     * 删除科目
     */
    @ResponseBody
    @RequestMapping("/deleteSubject")
    public ResultData deleteSubject(@RequestBody Subject subject){
        subjectService.deleteSubject(subject);
        return RetunResult.AssemblyData();
    }

    /**
     * 查询指定学历下面的所有年级
     */
    @ResponseBody
    @RequestMapping("/findAllGrade")
    public ResultData findAllGrade(@RequestBody Tlevel tlevel){
        return RetunResult.AssemblyData(subjectService.findAllGrade(tlevel));
    }

    /**
     * 查询指定年级下面的所有科目
     */
    @ResponseBody
    @RequestMapping("/findAllSubject")
    public ResultData findAllSubject(@RequestBody Grade grade){
        return RetunResult.AssemblyData(subjectService.findAllSubject(grade));
    }

    /**
     * 获取所有学历
     * @return
     */
    @ResponseBody
    @RequestMapping("/findAllLevel")
    public ResultData findAllLevel(){
        List<Tlevel> list = subjectService.findAllLevel();
        return RetunResult.AssemblyData(list);
    }

    /**
     * 编辑学历
     * @param tlevel
     * @return
     */
    @ResponseBody
    @RequestMapping("/editLevel")
    public ResultData editLevel(@RequestBody Tlevel tlevel){
        subjectService.editLevel(tlevel);
        return RetunResult.AssemblyData();
    }

    /**
     * 编辑年级
     * @param grade
     * @return
     */
    @ResponseBody
    @RequestMapping("/editGrade")
    public ResultData editGrade(@RequestBody Grade grade){
        subjectService.editGrade(grade);
        return RetunResult.AssemblyData();
    }

    @ResponseBody
    @RequestMapping("/editSubject")
    public ResultData editSubject(@RequestBody Subject subject){
        subjectService.editSubject(subject);
        return RetunResult.AssemblyData();
    }

}
